package cn.edu.sdu.sol.material_management_system.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class TokenRedisStorage {

    private static final String TOKEN_PREFIX = "auth:token:";
    private static final String PK_PREFIX = "auth:pk:";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 将 Token 存储到 Redis，设置过期时间
    public void saveToken(String str, String token, long expiration) {
        String key = TOKEN_PREFIX + str;
        redisTemplate.opsForValue().set(key, token, expiration, TimeUnit.SECONDS);
    }

    // 从 Redis 中获取 Token
    public String getToken(String str) {
        String key = TOKEN_PREFIX + str;
        return redisTemplate.opsForValue().get(key);
    }

    // 从 Redis 中删除 Token
    public void deleteToken(String str) {
        String key = TOKEN_PREFIX + str;
        redisTemplate.delete(key);
    }

}